import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'index.dart';

class CalendarViewController extends GetxController {
  CalendarViewController();

  final state = CalendarViewState();

  void initData() {
    var delta = DateTime.fromMillisecondsSinceEpoch(
        DateTime.now().millisecondsSinceEpoch);
    state.now
      ..year = delta.year
      ..month = MonthModel(
        month: delta.month,
        day: delta.day,
        days: _getCurrentMonthDays(year: delta.year, month: delta.month),
      );
    setupData(year: delta.year, month: delta.month, day: delta.day);
  }

  void setupData({
    required int year,
    required int month,
    required int day,
  }) {
    state.curr.value
      ..year = year
      ..month = MonthModel(
        month: month,
        day: day,
        days: _getCurrentMonthDays(year: year, month: month),
      );
    state.curr.value.months = List.generate(12, (index) => index + 1).map(
      (e) {
        var date = DateTime(year, e, 1);
        var days = _getCurrentMonthDays(year: year, month: e);
        var lastMonth = e == 1 ? 12 : (e - 1);
        var lastYear = year - (e == 1 ? 1 : 0);
        return MonthModel(
            month: e,
            day: -1,
            days: _getCurrentMonthDays(year: year, month: e),
            startWeekday: date.weekday,
            endWeekday: DateTime(year, e, days).weekday,
            lastMonthDays:
                _getCurrentMonthDays(year: lastYear, month: lastMonth));
      },
    ).toList();
    var currMonth = state.curr.value.months[state.curr.value.month.month - 1];
    state.curr.value.month.asyncData(currMonth);
  }

  // 获取当前月份天数
  int _getCurrentMonthDays({
    required int year,
    required int month,
  }) {
    if (month == 2) {
      if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
        return 29;
      } else {
        return 28;
      }
    } else if (month == 1 ||
        month == 3 ||
        month == 5 ||
        month == 7 ||
        month == 8 ||
        month == 10 ||
        month == 12) {
      return 31;
    } else {
      return 30;
    }
  }

  void actionNextMonth() {
    var curr = state.curr.value.month;
    if (curr.month == 12) {
      setupData(year: state.curr.value.year + 1, month: 1, day: 1);
    } else {
      state.curr.value.month =
          state.curr.value.months[state.curr.value.month.month];
    }
    state.curr.refresh();
  }

  void actionLastMonth() {
    var curr = state.curr.value.month;
    if (curr.month == 1) {
      setupData(year: state.curr.value.year - 1, month: 12, day: 1);
    } else {
      state.curr.value.month =
          state.curr.value.months[state.curr.value.month.month - 2];
    }
    state.curr.refresh();
  }

  void actionChoose(DateModel e) {
    if (state.start.value.month.day != 0 && state.end.value.month.day != 0) {
      state.start.value.month.day = 0;
      state.end.value.month.day = 0;
      state.start.value.take(e);
      state.start.refresh();
    } else if (state.start.value.month.day != 0 &&
        state.end.value.month.day == 0) {
      if (state.start.value.equal(e)) {
        state.start.value.month.day == 0;
        return;
      }
      if (!state.isRange) {
        state.start.value.take(e);
        state.start.refresh();
        return;
      }

      if (state.start.value.isAfter(e)) {
        state.end.value.take(state.start.value);
        state.start.value.take(e);
        state.start.refresh();
      } else {
        state.end.value.take(e);
        state.end.refresh();
      }
    } else {
      state.start.value.take(e);
      state.start.refresh();
    }
  }

  void actionShowYear() {
    state.isShowYearView = true;
    update();
  }

  void actionChooseYear(int y) {
    state.isShowYearView = false;
    update();
    setupData(
        year: y,
        month: state.curr.value.month.month,
        day: state.curr.value.month.day);
  }

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    super.onInit();
    initData();
    Future.delayed(Duration(milliseconds: 200)).then((value) {
      state.bgColor = Colors.black45;
      update();
    });
  }

  void back() {
    state.bgColor = Colors.transparent;
    update();
    Get.back();
  }

  void actionSure() {
    var start = state.start.value;
    var end = state.end.value;
    if (state.isRange) {
      if (start.month.day == 0 && end.month.day == 0) {
        return;
      }
    } else {
      if (start.month.day == 0) {
        return;
      }
    }
    var st = DateTime(start.year, start.month.month, start.month.day);
    var et = DateTime(end.year, end.month.month, end.month.day);
    state.result(st, et);
    back();
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    super.onReady();
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
    state.start.close();
    state.end.close();
    state.curr.close();
  }

  /// dispose 释放内存
  @override
  void dispose() {
    super.dispose();
  }
}
